package com.springboot.controller;

import com.springboot.model.UserLoginForm;
import com.springboot.service.UserLoginService;
import com.springboot.util.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 本控制台用于用户的忘记密码
 */
@Controller
public class UserForgetController {
    //声明业务类
    @Autowired
    UserLoginService userLoginService;

    //患者端忘记密码界面响应,通过@RequestParam获取参数,获得验证码
    @CrossOrigin
    @RequestMapping(value = "/patient_authcode")//get  @getMapping    post  @postMapping
    public @ResponseBody
    String patientAuthCode(@RequestParam("phone") String tel)
    {
        System.out.println(tel + " " + "正在获取验证码");
        try {
            UserLoginForm user= userLoginService.findUserByTel(tel);
            if(user.getUserType()==1)
            {
                SendMessage send=new SendMessage(); //获得验证码
                return send.sendMessage(tel);
            }
            else
            {
                return "False";
            }
        }catch(Exception e)
        {
            return "False";
        }
    }
    //患者端忘记密码界面响应,通过@RequestParam获取参数,更改密码
    @CrossOrigin
    @RequestMapping(value = "/patientforget")//get  @getMapping    post  @postMapping
    public @ResponseBody
    String patientConfirmPassword(@RequestParam("phone") String tel,
                                  @RequestParam("pass") String confirmPassword)
    {
        try {
            UserLoginForm user= userLoginService.findUserByTel(tel);
            if(user.getUserType()==1)
            {
                user.setUserPwd(confirmPassword); //更新内存
                userLoginService.updatePassword(user);  //更新数据库
                return "True";
            }
            else
            {
                return "False";
            }
        }catch(Exception e)
        {
            return "False";
        }
    }


    //医生端和助理端忘记密码界面响应,通过@RequestParam获取参数,获得验证码
    @CrossOrigin
    @RequestMapping(value = "/getCode")//get  @getMapping    post  @postMapping
    public @ResponseBody
    String doctorAuthCode(@RequestParam("tel") String tel)
    {
        System.out.println(tel + " " + "正在获取验证码");
        try {
            UserLoginForm user= userLoginService.findUserByTel(tel);
            SendMessage send=new SendMessage();
            return send.sendMessage(tel);
        }catch(Exception e)
        {
            return "False";
        }
    }
    //医生端和助理端忘记密码界面响应,通过@RequestParam获取参数,更改密码
    @CrossOrigin
    @RequestMapping(value = "/resetPassw")//get  @getMapping    post  @postMapping
    public @ResponseBody
    String doctorConfirmPassword(@RequestParam("phone") String tel,
                                 @RequestParam("pass") String confirmPassword)
    {
        try {
            UserLoginForm user= userLoginService.findUserByTel(tel);
            user.setUserPwd(confirmPassword);
            userLoginService.updatePassword(user);
            System.out.println(user.getUserPwd());
            return "True";
        }catch(Exception e)
        {
            return "False";
        }
    }
}